/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-02-06     HZHJYYJQR       the first version
 */


#include "HJRfCardDevice.h"
#include <HJComm/HJRfCard.h>
#include <BSP/bsp_uart2.h>
#include <App/app_nfc.h>
#include <App/app_comm.h>
#include <BSP/bsp_uart6.h>
#include "HJFrame.h"

class Frame rf_frame;

class HJRfCardDevice {

public:

    HJRfCardDevice();

    virtual ~HJRfCardDevice();

    void RxProcess(uint8_t data, uint16_t len);
private:

    void TxTransmit(uint8_t* buff, uint16_t len);

    void OnRecvOneFram(RFCard* rfcard);
    RFCardRecv m_rfCardRecv;
    RFCard m_rfcard;
};

void HJRfCardDevice::TxTransmit(uint8_t* buff, uint16_t len)
{
    bsp_uart2_tx_transmit(buff, len);
}

HJRfCardDevice::HJRfCardDevice(void)
{
}

HJRfCardDevice::~HJRfCardDevice()
{
}

void HJRfCardDevice::OnRecvOneFram(RFCard* rfcard)
{
    uint16_t len=0;
    if((rfcard->len ==6) && (rfcard->data[0] !=0)){
        rt_kprintf("card connect error...\r\n"); m_nfc.Actual_Step=0;m_nfc._string=(char *)"nfc_error";return;
    }//写卡失败,重新寻卡
    switch(rfcard->cmd)
    {
        case FindCardCmd: //寻卡
           m_nfc.cardtype[0]=rfcard->data[1];
           m_nfc.cardtype[1]=rfcard->data[2];
           if(m_nfc.cardtype[1]==0)m_nfc.cardtype_len=1;
           m_nfc.Actual_Step=1;
           m_nfc._string=(char *)"nfc_error";
           rt_kprintf("find card ok\r\n");
           break;
        case AntiCollisionCmd: //防冲突
           m_nfc.cardnum_len= rfcard->data_len -1;
           for(uint8_t i=0;i<m_nfc.cardnum_len;i++)m_nfc.cardnum[i]=rfcard->data[1+i];
           m_nfc.Actual_Step=2;
           m_nfc._string=(char *)"nfc_error";
           rt_kprintf("AntiCollision ok\r\n");
           break;
        case SelectCardCmd: // 选卡
           m_nfc.Actual_Step=3;
           m_nfc._string=(char *)"error";
           rt_kprintf("select card ok\r\n");
           break;
        case attestateCmd:  //认证
            m_nfc.Actual_Step=4;
            m_nfc._string=(char *)"nfc_error";
            rt_kprintf("CardCertificate ok\r\n");
            break;
        case ReadCardCmd:   //读卡
//           m_nfc.Actual_Step=5;
            m_nfc._string = (char*)(rfcard->data+1);
            rt_kprintf("%s\n",m_nfc._string);
            rt_kprintf("read card ok\r\n");
            break;
        case WriteCardCmd://写卡
            if(rfcard->data[0] != 0){
                m_nfc.Actual_Step =0 ;
                rt_kprintf("card connect error...\r\n");//写卡失败,重新寻卡
            }
            else{
                if(m_nfc.Actual_Step==5)m_nfc.Actual_Step=4;;
                rt_kprintf("write card ok\r\n");
            }
            break;
        case ReadValueCmd://读值
            break;
        case ReChargeCmd://充值
            break;
        case DecreaseValueCmd://减值
            break;
        default:break;
    }
}

void HJRfCardDevice::RxProcess(uint8_t data, uint16_t len)
{
    if (m_rfCardRecv.proc_one_byte(&m_rfcard, data))
    {
//        rt_kprintf("接收到一帧 处理帧");
        OnRecvOneFram(&m_rfcard);
    }
}

void HJRfCardHandleWrap(void)
{
    static HJRfCardDevice rfcard_device;
    uint8_t data;
    bsp_uart2_rx_transmit(&data, RT_WAITING_FOREVER);
    rfcard_device.RxProcess(data, 1);
}

/* 线程入口 */
static void RfCardthread_recv_entry(void* parameter)
{
    while (1)
    {
        HJRfCardHandleWrap();
//        rt_thread_mdelay(10); // 延时一段时间
    }
}

/* 线程入口 */
static void RfCardthread_send_entry(void* parameter)
{
    while (1)
    {
        switch(m_nfc.Actual_Step)
        {
            case 0://寻卡
                rt_kprintf("find card...\r\n");
                nfc_findcard_send();

                break;
            case 1://防冲突
                rt_kprintf("set AntiCollision...\r\n");
                nfc_AntiCollisionCmd_send(m_nfc.cardtype,m_nfc.cardtype_len);

                break;
            case 2://选卡
                rt_kprintf("select card...\r\n");
                nfc_SelectCardCmd_send(m_nfc.cardnum,m_nfc.cardnum_len);

                break;
            case 3://认证
                rt_kprintf("Certificate card...\r\n");
                nfc_CardCertificateCmd_send(0x60,1);

                break;
            case 4://一键读卡
                rt_kprintf("read card...\r\n");
                nfc_OneKeyReadCardCmd_send(0x01);

                break;
            case 5://一键写卡
                rt_kprintf("write card...\r\n");
//                nfc_OneKeyWriteCardCmd_send(m_nfc.total,m_nfc.remain,0x01);
                nfc_OneKeyWriteCardCmd(m_nfc._string,0x01);
                break;
        }
        rt_thread_mdelay(100); // 延时一段时间
    }
}


/* 线程入口 */
static void pcthread_send_entry(void* parameter)
{
    char Json_rep_buf[1024]={0};
    while (1)
    {
        uint8_t tx_buff[1044] = {0};
        uint16_t txlen = 0;

//        Json_tx(Json_rep_buf, sizeof(Json_rep_buf));
//        bsp_uart6_tx_transmit((uint8_t *) Json_rep_buf,  strlen(Json_rep_buf));

        Json_tx(Json_rep_buf, sizeof(Json_rep_buf));
        rf_frame.paload_len = strlen(Json_rep_buf);
        memcpy(rf_frame.paload,Json_rep_buf,rf_frame.paload_len);

        rf_frame.make_pack(tx_buff, &txlen);
        bsp_uart6_tx_transmit(tx_buff,txlen);

        rt_thread_mdelay(50); // 延时一段时间
    }
}

void RfCardrev_thread_init(void)
{

    m_nfc.Actual_Step =0 ;
//    m_nfc.total = 10;
//    m_nfc.remain = 5;

    rt_thread_t tid = RT_NULL;
    /* 创建线程 1 */
    tid = rt_thread_create("rfcard_recv",
                            RfCardthread_recv_entry, RT_NULL,
                            1024,
                            11, 10);
    if (tid != RT_NULL)
        rt_thread_startup(tid);

    rt_thread_t tid2 = RT_NULL;
    /* 创建线程 1 */
    tid2 = rt_thread_create("rfcard_send",
                            RfCardthread_send_entry, RT_NULL,
                            1024,
                            11, 10);
    if (tid2 != RT_NULL)
        rt_thread_startup(tid2);

    rt_thread_t tid3 = RT_NULL;
    /* 创建线程 1 */
    tid3 = rt_thread_create("pc_tx",
                            pcthread_send_entry, RT_NULL,
                            4096,
                            16, 10);
    if (tid3 != RT_NULL)
        rt_thread_startup(tid3);
}
